package com.tdcy.biz.scmgt.controller;

import java.io.IOException;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tdcy.biz.scmgt.service.bean.PicTextBean;
import com.tdcy.biz.wxmgt.service.WeixinService;
import com.tdcy.framework.BaseController;
import com.tdcy.framework.bean.PageInfo;
import com.tdcy.framework.util.WebUtils;

/**
*/
@Controller
@RequestMapping(value = "/pictext")
public class WxPicTextController extends BaseController {
	@Autowired
	WeixinService wxMpService;

	@ResponseBody
	@RequestMapping(value = "/materialNewsBatchGet", method = RequestMethod.POST)
	public Object materialNewsBatchGet(PicTextBean icTextBean) throws IOException,
			WxErrorException {
		WxMpMaterialNewsBatchGetResult result = wxMpService.getMaterialService().materialNewsBatchGet(icTextBean.getStartRow(), icTextBean.getRows());
		int total =  result.getTotalCount();

		int totalPage = 1;
		if (total % 1000 == 0) {
			totalPage = (int) total / 1000;
		} else {
			totalPage = (int) total / 1000 + 1;
		}
		
		PageInfo<WxMaterialNewsBatchGetNewsItem> pageData = new PageInfo<WxMaterialNewsBatchGetNewsItem>();
		pageData.setTotalRecord((int) result.getTotalCount());
		pageData.setTotalPage(totalPage);
		pageData.setResultsList(result.getItems());
		pageData.setPageSize(icTextBean.getRows());
		pageData.setCurPageNO(icTextBean.getPage());
		
		return WebUtils.createJSONSuccess("更新成功");
	}
}
